<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cake.dao.CategoryDao"%>
<%@page import="cake.dao.CakeDao"%>
<%@page import="cake.bean.Cake"%>
<%@page import="cake.bean.Category"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>cakes page</title>
		<link rel="stylesheet" href="styles/style.css" type="text/css" />
		<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
		<script src="scripts/jquery.select.js" type="text/javascript"></script>
		<script src="scripts/tools.js" type="text/javascript"></script>
		<script src="scripts/script.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="container">
			<div id="header">
				<%@include file="header.jsp" %>
			</div>
			<div id="content">
				<div style="margin-left: 35px;">
					<div>
						Location：
						<a href="index.jsp">Home</a> &gt; cakes show
					</div>
					<div style="padding: 40px 50px 40px 100px;" class="cakes_bg">
						<div
							style="float: left; border-right: solid 1px Gray; padding: 20px;">
							<ul>
								<li>
									<a href="cakes.jsp">All</a>
								</li>
								<%
									CategoryDao categoryDao = new CategoryDao();
									List<Category> list = categoryDao.getCategorys(1, 0, "");
									if (list.size() > 0) {
										for (Category c : list) {
								%>
								<li>
									<a href="cakes.jsp?page=1&category=<%=c.getId()%>"><%=c.getName()%></a>
								</li>
								<%
									}
									}
								%>
							</ul>
						</div>
						<div style="width: 600px; padding-left: 20px;">
							<%--<div style="border-bottom:solid 1px Red;">
										<span><a href="cakes.jsp?page=1">[FirstPage]</a></span>
										<span><a href="cakes.jsp?page=1">1</a></span>	
										<span><a href="cakes.jsp?page=2">2</a></span>
										<span><a href="cakes.jsp?page=3">3</a></span>
										<span><a href="cakes.jsp?page=4">[LastPage]</a></span>
									</div>
									--%>
							<%
								String currPage = request.getParameter("page");
								String category = request.getParameter("category");
								String keywords=request.getParameter("keywords");
								int pageSize=5;
								int pageNum = 1;
								long categoryId = 0;
								String where = " valid = 1 ";
								if (currPage != null && !currPage.equals("")) {
									pageNum = Integer.parseInt(currPage);
								}
								if (category != null && !category.equals("")) {
									categoryId = Long.parseLong(category);
								}
								if (categoryId != 0) {
									where += " and category=" + categoryId;
								}
								
								if(keywords!=null && !keywords.equals(""))
								{
									if(where.equals(""))
									{
										where +=" name like '%"+keywords+"%' ";
									}
									else
									{
										where +=" and name like '%"+keywords+"%' ";
									}
								}
								CakeDao cakeDao = new CakeDao();

								int total = cakeDao.getTotalPage(where);
								int totalPage=0;
								if(total%pageSize==0)
								{
									totalPage=total/pageSize;
								}
								else
								{
									totalPage=total/pageSize+1;
								}
								if (totalPage > 1) {
									for (int i = 1; i <= totalPage; i++) {
										if (i == 1) {
							%>
							<span><a href="cakes.jsp?page=<%=i%>&category=<%=categoryId %>">[FirstPage]</a>
							</span>
							<%
								} else if (i == totalPage) {
							%>
							<span><a href="cakes.jsp?page=<%=i%>&category=<%=categoryId %>">[LastPage]</a>
							</span>
							<%
								} else {
							%>
							<span><a href="cakes.jsp?page=<%=i%>&category=<%=categoryId %>"><%=i%></a>
							</span>
							<%
								}
									}
								}
								List<Cake> cakeList = cakeDao.getCakes(pageNum, pageSize, where);
								if (cakeList.size() > 0) {
									for (Cake c : cakeList) {
							%>
							<div
								style="border-bottom: dashed 1px Gray; margin-top: 5px; width: 95%; clear: both;">
								<div style="float: left">
									<img src="<%=c.getPicture()%>" alt="" style="width:160px;height:160px;"/>
								</div>
								<div>
										<b>name:</b>  <%=c.getName()%><br />
										<b>price:</b>  <%=c.getPrice()%><br />
										<b>remark:</b>  <%=c.getRemark()%><br />
										<%
					    				if(username==null || username.equals(""))
					    				{
										%>
										<input type="button" value="order" class="btn" onclick="toURL('login.jsp');" /><br/>
										quantity:<input type="text" value="1" class="quantity" id="q<%=c.getId() %>" /><br/>
										<input type="button" value="addToCart" class="btn2" onclick="toURL('login.jsp');" />
										<%
					    				}
					    				else
					    				{
										%>
										<input type="button" value="order" class="btn" onclick="toURL('order.jsp?cake=<%=c.getId() %>');" /><br/>
										quantity:<input type="text" value="1" class="quantity" id="q<%=c.getId() %>" /><br />
										<input type="button" value="addToCart" class="btn2" onclick="addToCart(<%=c.getId() %>);" />
										<%
					    				}
										%>
										<br /><a href="comments.jsp?cake=<%=c.getId() %>" target="blank">see comments</a>
								</div>
							</div>
							<%
								}
								}
							%>
						</div>
					</div>
				</div>
				<div id="categorys" style="clear:both;">
					<%@include file="footer.jsp" %>
				</div>
			</div>
		</div>
	</body>
</html>
